import { usePhotoGallery } from '@/composables/usePhotoGallery';

// 模拟 Capacitor Camera
jest.mock('@capacitor/camera', () => ({
  Camera: {
    getPhoto: jest.fn()
  },
  CameraResultType: {
    Uri: 'uri'
  },
  CameraSource: {
    Camera: 'camera'
  }
}));

// 模拟 Capacitor Filesystem
jest.mock('@capacitor/filesystem', () => ({
  Filesystem: {
    writeFile: jest.fn(),
    readFile: jest.fn(),
    deleteFile: jest.fn()
  },
  Directory: {
    Data: 'data'
  }
}));

describe('usePhotoGallery', () => {
  let photoGallery: any;

  beforeEach(() => {
    photoGallery = usePhotoGallery();
  });

  describe('拍照功能', () => {
    it('应该能够在 HarmonyOS 环境中拍照', async () => {
      // 设置 HarmonyOS 环境
      (window as any).HarmonyOS = true;
      (window as any).HarmonyBridge = {
        camera: {
          open: jest.fn().mockResolvedValue({ success: true }),
          takePhoto: jest.fn().mockResolvedValue({
            webPath: 'mock-photo.jpg',
            format: 'jpeg'
          })
        }
      };

      await photoGallery.takePhoto();

      expect(window.HarmonyBridge.camera.takePhoto).toHaveBeenCalled();
    });

    it('应该能够在 Web 环境中拍照', async () => {
      // 设置 Web 环境
      (window as any).HarmonyOS = false;
      
      const { Camera } = require('@capacitor/camera');
      Camera.getPhoto.mockResolvedValue({
        webPath: 'mock-web-photo.jpg',
        format: 'jpeg'
      });

      await photoGallery.takePhoto();

      expect(Camera.getPhoto).toHaveBeenCalledWith({
        resultType: 'uri',
        source: 'camera',
        quality: 90,
        width: 600,
        height: 600
      });
    });

    it('应该处理拍照错误', async () => {
      const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
      
      // 模拟拍照失败
      (window as any).HarmonyBridge = {
        camera: {
          takePhoto: jest.fn().mockRejectedValue(new Error('相机不可用'))
        }
      };

      await photoGallery.takePhoto();

      expect(consoleSpy).toHaveBeenCalledWith('拍照失败:', expect.any(Error));
      consoleSpy.mockRestore();
    });
  });

  describe('照片存储', () => {
    it('应该能够保存照片', async () => {
      const mockPhoto = {
        webPath: 'mock-photo.jpg',
        format: 'jpeg'
      };

      const { Filesystem } = require('@capacitor/filesystem');
      Filesystem.writeFile.mockResolvedValue({ uri: 'saved-photo.jpg' });

      const result = await photoGallery.savePicture(mockPhoto, 'test.jpg');

      expect(Filesystem.writeFile).toHaveBeenCalled();
      expect(result).toBeDefined();
    });
  });

  describe('平台检测', () => {
    it('应该正确检测 HarmonyOS 平台', () => {
      // 测试 HarmonyOS 检测逻辑
      (window as any).HarmonyOS = true;
      expect(photoGallery.isHarmonyOS()).toBe(true);

      (window as any).HarmonyOS = false;
      navigator.userAgent = 'Mozilla/5.0 (compatible; HarmonyOS)';
      expect(photoGallery.isHarmonyOS()).toBe(true);

      (window as any).HarmonyBridge = {};
      expect(photoGallery.isHarmonyOS()).toBe(true);
    });
  });
});